package cn.itcast.poi.service.impl;

import cn.itcast.poi.dao.UserDao;
import cn.itcast.poi.entity.User;
import cn.itcast.poi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public void insertList(List<User> userList) {
        userDao.saveAll(userList);
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }


    /**
     * 导入数据之前的校验
     *
     * @param userList 数据集合
     * @return Map<String, Object>
     */
    @Override
    public Map<String, Object> checkData(List<User> userList) {
        boolean flag = true;
        List<User> users = new LinkedList<>();
        for (User user : userList) {
            if (user.getAge() > 85) {
                user.setRemark("年龄大于88岁,哈哈哈...");
                flag = false;
            }
            users.add(user);
        }
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("flag", flag);
        resultMap.put("records", users);
        return resultMap;
    }
}
